package com.yichi;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.yichi.global.AppConfig;
import com.yichi.manager.OnHttpResponseListener;
import com.yichi.service.PlatformService;
import com.yichi.util.ToastUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class AddressActivity extends AppCompatActivity {

    private TextView btnSave;
    private ImageView iv_back;
    private EditText et_ipAddress;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_address);

        iv_back = findViewById(R.id.iv_back);
        btnSave = findViewById(R.id.btn_Save);
        et_ipAddress = findViewById(R.id.et_ipAddress);

        et_ipAddress.setText(AppConfig.PLATFORM_BASEURL);

        //保存
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("".equals(et_ipAddress.getText().toString().trim())) {
                    Toast.makeText(AddressActivity.this, "请输入地址", Toast.LENGTH_SHORT).show();
                    return;
                }

                String baseUrl = et_ipAddress.getText().toString().trim();
                //测试链接
                try {
                    PlatformService.testConn(baseUrl, new OnHttpResponseListener() {
                        @Override
                        public void success(Call call, Response response) throws IOException {
                            AppConfig.savePlaformBaseURL(baseUrl);
                            runOnUiThread(()->{
                                ToastUtil.show(AddressActivity.this, "保存成功！");
                            });
                        }

                        @Override
                        public void failed(Call call, IOException e) {
                            runOnUiThread(()->{
                                ToastUtil.show(AddressActivity.this, "连接失败：" + e.getMessage());
                            });
                        }
                    });
                } catch (Exception e) {
                    runOnUiThread(()->{
                        ToastUtil.show(AddressActivity.this, "连接失败：" + e.getMessage());
                    });
                }
            }
        });

        //返回
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
